export function formatPages(files, dir) {
  // 获取最大的章节号
  const lastTitle = files[files.length - 1]
  const max = Number(lastTitle.match(/^[A-Z](\d+)-/)[1])

  const pages = []
  for (let i = 0; i < max + 1; i++) {
    const page = {
      chapter: '',
      articles: []
    }
    files.forEach((item, index) => {
      let chapter = ''
      const article = {}
      const num = String(i + 1).padStart(2, '0')

      // 过滤掉以"-API"结尾的文件
      // if (/-API$/i.test(item)) {
      //   return
      // }
      item = item?.replace(/.md$/, '')

      if (new RegExp(`^[A-Z]${num}`, 'i').test(item)) {
        if (!item.match(/^[A-Z](?![16|30])\d+-00 (.*?)-/)) {
          chapter = item.match(/^[A-Z]\d+-\d+ (.*?)-/)[1]
        }
        article.title = item
        article.url = `/doc/${dir}/${item}.html`
        if (!page.chapter) {
          page.chapter = chapter
          page.api = `/doc/${dir}/API/${chapter}/`
        }
        page.articles.push(article)
      }
    })
    pages.push(page)
  }

  // console.log(pages)
  return pages
}
